import {defineStore} from "pinia"

import {
    getCategoryRequest1,
    getCategoryRequest2,
    getCategoryRequest3,
} from "@/api/attr";

import type {getCategoryType} from '@/api/attr'
import { ElMessage } from "element-plus";

//state的类型限制
interface storeType {
    categoryList1: getCategoryType;
    categoryList2: getCategoryType;
    categoryList3: getCategoryType;
    category1Id: number | null;
    category2Id: number | null;
    category3Id: number | null;
}

const useAttrStore = defineStore("attr",{
    state:():storeType => {
        return {
            categoryList1 : [],
            categoryList2 : [],
            categoryList3 : [],
            category1Id : null,
            category2Id : null,
            category3Id : null,
        }
    },

    getters:{},
    actions:{
        //获取1级列表
        async getCategoryList1(){
            try {
                //发送请求
                const re = await getCategoryRequest1();
                //把一级分类的值赋值给state
                this.categoryList1 = re
            } catch (e) {
                ElMessage.error("请求一级分类失败")
            }
        },
        //获取2级列表
        async getCategoryList2(){
            try {
                //发送请求
                const re = await getCategoryRequest2(this.category1Id as number);
                //把一级分类的值赋值给state
                this.categoryList2 = re;
            } catch (e) {
                ElMessage.error("请求二级分类失败")
            }
        },
        //获取3级列表
        async getCategoryList3(){
            try {
                //发送请求
                const re = await getCategoryRequest3(this.category2Id as number);
                //把一级分类的值赋值给state
                this.categoryList3 = re;
            } catch (e) {
                ElMessage.error("请求三级级分类失败")
            }
        }
    }
})

export default useAttrStore